package org.jetlinks.community.consumer;

import lombok.extern.slf4j.Slf4j;
import org.jetlinks.community.command.CommandEnum;
import org.jetlinks.community.constant.RabbitmqConstant;
import org.jetlinks.community.entity.DeviceBizMessageReply;
import org.jetlinks.community.gateway.annotation.Subscribe;
import org.jetlinks.community.utils.BizMessageUtil;
import org.jetlinks.core.message.DeviceMessage;
import org.jetlinks.core.message.DeviceOfflineMessage;
import org.jetlinks.core.message.DeviceOnlineMessage;
import org.jetlinks.core.message.DirectDeviceMessage;
import org.jetlinks.core.message.event.EventMessage;
import org.jetlinks.core.message.function.FunctionInvokeMessage;
import org.jetlinks.core.message.function.FunctionInvokeMessageReply;
import org.jetlinks.core.message.property.*;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@Component
@Slf4j
public class DeviceMsgConsumer {
    @Autowired
    public AmqpTemplate amqpTemplate;

    // * 监听消息
    // * 通配符**表示匹配多层路径,*表示匹配单层路径. 不支持前后匹配,如: /device/id-*/message.
    //   发布和订阅均支持通配符,发布时使用通配符时则进行广播.
    //所有设备消息的topic的前缀均为: /device/{productId}/{deviceId}.
    //如:产品product-1下的设备device-1上线消息: /device/product-1/device-1/online.
    /**
     * @param message
     * @return
     */
    @Subscribe("/device/**")
    public Mono<Void> handlerMsg(DeviceMessage message){
        if(log.isDebugEnabled()){
            log.debug("测试接收消息【{}】",message);
        }
        return Mono.justOrEmpty(message)
            .map(msg->{
                if(msg instanceof DeviceOnlineMessage){//设备上线

                }else if(msg instanceof DeviceOfflineMessage){//设备下线

                }else if(msg instanceof EventMessage){//设备下线
                    EventMessage event=(EventMessage)message;
                    String enumType=event.getEvent();
                    /*String deviceId=event.getDeviceId();
                    String messageId=event.getMessageId();*/

                    //低电通知
                    if(CommandEnum.BLOW.name().equals(enumType)){
                        /*Map<String, Object> blowMap = (Map<String, Object>)event.getData();
                        blowMap.put("enumType",enumType);
                        blowMap.put("deviceId",deviceId);
                        blowMap.put("messageId",messageId);*/
//                        publishDeviceMessageToRabbitmq(blowMap,RabbitmqConstant.SENT_TOPIC_PREFIX+enumType+RabbitmqConstant.SENT_TOPIC_BLOW);
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromEventMessage(event));
                    }

                    //通话记录上报 CALLNOTE
                    if(CommandEnum.CALLNOTE.name().equals(enumType)){
                        /*Map<String, Object> callnoteMap = (Map<String, Object>)event.getData();
                        callnoteMap.put("enumType",enumType);
                        callnoteMap.put("deviceId",deviceId);
                        callnoteMap.put("messageId",messageId);*/
//                        publishDeviceMessageToRabbitmq(callnoteMap,RabbitmqConstant.SENT_TOPIC_PREFIX+enumType+RabbitmqConstant.SENT_TOPIC_CALLNOTE);
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromEventMessage(event));
                    }

                    //位置数据 V2
                    if(CommandEnum.V2.name().equals(enumType)){
                        /*Map<String, Object> v2Map = (Map<String, Object>)event.getData();
                        v2Map.put("enumType",enumType);
                        v2Map.put("deviceId",deviceId);
                        v2Map.put("messageId",messageId);*/
//                        publishDeviceMessageToRabbitmq(v2Map,RabbitmqConstant.SENT_TOPIC_PREFIX+enumType+RabbitmqConstant.SENT_TOPIC_LOCATIONDATA);
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromEventMessage(event));
                    }

                    //数据补传 V3
                    if(CommandEnum.V3.name().equals(enumType)){
                       /* Map<String, Object> v3Map = (Map<String, Object>)event.getData();
                        v3Map.put("enumType",enumType);
                        v3Map.put("deviceId",deviceId);
                        v3Map.put("messageId",messageId);*/
//                        publishDeviceMessageToRabbitmq(v3Map,RabbitmqConstant.SENT_TOPIC_PREFIX+enumType+RabbitmqConstant.SENT_TOPIC_DATAFILL);
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromEventMessage(event));
                    }

                    //WIFI 到校/离校状态 SCHOOLSTATUS
                    if(CommandEnum.SCHOOLSTATUS.name().equals(enumType)){
                       /* Map<String, Object> schoolstatusMap = (Map<String, Object>)event.getData();
                        schoolstatusMap.put("enumType",enumType);
                        schoolstatusMap.put("deviceId",deviceId);
                        schoolstatusMap.put("messageId",messageId);*/
//                        publishDeviceMessageToRabbitmq(schoolstatusMap,RabbitmqConstant.SENT_TOPIC_PREFIX+enumType+RabbitmqConstant.SENT_TOPIC_SCHOOLSTATUS);
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromEventMessage(event));
                    }

                    //回家/离家状态 HOMESTATUS
                    if(CommandEnum.HOMESTATUS.name().equals(enumType)){
                        /*Map<String, Object> homestatusMap = (Map<String, Object>)event.getData();
                        homestatusMap.put("enumType",enumType);
                        homestatusMap.put("deviceId",deviceId);
                        homestatusMap.put("messageId",messageId);*/
//                        publishDeviceMessageToRabbitmq(homestatusMap,RabbitmqConstant.SENT_TOPIC_PREFIX+enumType+RabbitmqConstant.SENT_TOPIC_HOMESTATUS);
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromEventMessage(event));
                    }

                    //温度上报 TEMPERTURE
                    if(CommandEnum.TEMPERTURE.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromEventMessage(event));
                    }

                    //温度上报 TEMPERTUREM
                    if(CommandEnum.TEMPERTUREM.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromEventMessage(event));
                    }

                    //关机通知 POWEROFF
                    if(CommandEnum.POWEROFF.name().equals(enumType)){
                       /* Map<String, Object> poweroffMap = (Map<String, Object>)event.getData();
                        poweroffMap.put("enumType",enumType);
                        poweroffMap.put("deviceId",deviceId);
                        poweroffMap.put("messageId",messageId);*/
//                        publishDeviceMessageToRabbitmq(poweroffMap,RabbitmqConstant.SENT_TOPIC_PREFIX+enumType+RabbitmqConstant.SENT_TOPIC_POWEROFF);
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromEventMessage(event));
                    }

                    //白名单主动获取 GETPBWLALL
                    if(CommandEnum.GETPBWLALL.name().equals(enumType)){
                     publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromEventMessage(event));
                    }

                    //白名单主动获取 GETWLALL
                    if(CommandEnum.GETWLALL.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromEventMessage(event));
                    }

                    //短信上报 GETSMS
                    if(CommandEnum.GETSMS.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromEventMessage(event));
                    }


                    //设备主动上报计步数据
                    if(CommandEnum.STEP.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromEventMessage(event));
                    }

                    //设备主动上报计步数据
                    /*if(CommandEnum.STEPS.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromEventMessage(event));
                    }*/

                    //异常状态上报 HWFUALT
                    if(CommandEnum.HWFUALT.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromEventMessage(event));
                    }

                    //监听记录上报 MONITORNOTE
                    if(CommandEnum.MONITORNOTE.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromEventMessage(event));
                    }

                    //终端设备上传电子签到通知信息 POSTIN
                    if(CommandEnum.POSTIN.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromEventMessage(event));
                    }

                    //终端设备上传电子签退通知信息 POSTOUT
                    if(CommandEnum.POSTOUT.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromEventMessage(event));
                    }

                    //主动上报开机指令 POWERON
                    if(CommandEnum.POWERON.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromEventMessage(event));
                    }

                    //拒接号码上报 REJECTNOTE
                    if(CommandEnum.REJECTNOTE.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromEventMessage(event));
                    }

                    //B、多围栏联动签到/签退信息（仅 TJ 设备适用） SCHOOLIO
                    if(CommandEnum.SCHOOLIO.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromEventMessage(event));
                    }

                    //休眠通知 SLEEPSTATUS
                    if(CommandEnum.SLEEPSTATUS.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromEventMessage(event));
                    }

                    //SOS 报警协议 SOS
                    if(CommandEnum.SOS.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromEventMessage(event));
                    }

                    //静止报警协议格式 STATIC
                    if(CommandEnum.STATIC.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromEventMessage(event));
                    }


                }else if(msg instanceof ReportPropertyMessage){//设备上报属性
                    ReportPropertyMessage report = (ReportPropertyMessage)message;
                    String enumType=report.getCode();

                    //定位辅助请求
                    if(CommandEnum.GETGPS.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromReportPropertyMessage(report));
                    }

                    //心跳数据
                    if(CommandEnum.KA.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromReportPropertyMessage(report));
                    }

                    //短信息状态同步 SMSSTATUS
                    if(CommandEnum.SMSSTATUS.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromReportPropertyMessage(report));
                    }

                    //WIFI 定位 WIFIMAC
                    if(CommandEnum.WIFIMAC.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromReportPropertyMessage(report));
                    }


//                    Map<String,Object> callaa = new HashMap();
//                    call.put("jingweidu",properties.get("jingweidu"));
//                    this.publishDeviceMessageToRabbitmq(callaa,"iot2biz.cmd.location");

                }else if(msg instanceof ReadPropertyMessage){//平台下发读取消息指令

                }else if(msg instanceof WritePropertyMessage){//平台下发修改消息指令

                }else if(msg instanceof ReadPropertyMessageReply){//读取属性回复

                }else if(msg instanceof WritePropertyMessageReply){//修改属性回复

                }else if(msg instanceof FunctionInvokeMessage){//平台下发功能调用


                }else if(msg instanceof FunctionInvokeMessageReply){//调用功能回复
                    FunctionInvokeMessageReply obj = (FunctionInvokeMessageReply)message;
                    String enumType=obj.getFunctionId();
                   /* String deviceId=obj.getDeviceId();
                    String messageId=obj.getMessageId();

                     DeviceBizMessageReply deviceBizMessageReply = new DeviceBizMessageReply();
                     deviceBizMessageReply.setReplyMessageId(messageId);
                     deviceBizMessageReply.setMessageId(messageId);


                    Map<String, Object> map = (Map<String, Object>)obj.getOutput();
                    publishDeviceMessageToRabbitmq(deviceBizMessageReply);*/



                    //亲情号同步
                    if(CommandEnum.WLALL.name().equals(enumType)){
                        /*Map<String, Object> wlallMap = (Map<String, Object>)obj.getOutput();
                        wlallMap.put("enumType",enumType);
                        wlallMap.put("deviceId",deviceId);
                        wlallMap.put("messageId",messageId);*/

//                        publishDeviceMessageToRabbitmq(wlallMap,RabbitmqConstant.SENT_TOPIC_PREFIX+enumType+RabbitmqConstant.SENT_TOPIC_FAMILIARITY);
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }
                    //设备绑定SIMICCID
                    if(CommandEnum.SETICCID.name().equals(enumType)){
                       /* Map<String, Object> iccIdMap = (Map<String, Object>)obj.getOutput();
                        iccIdMap.put("enumType",enumType);
                        iccIdMap.put("deviceId",deviceId);
                        iccIdMap.put("messageId",messageId);*/
//                        publishDeviceMessageToRabbitmq(iccIdMap,RabbitmqConstant.SENT_TOPIC_PREFIX+enumType+RabbitmqConstant.SENT_TOPIC_ICCID);
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }
                    //学生设备重启R1
                    if(CommandEnum.R1.name().equals(enumType)){
                       /* Map<String, Object> restartMap = (Map<String, Object>)obj.getOutput();
                        restartMap.put("enumType",enumType);
                        restartMap.put("deviceId",deviceId);
                        restartMap.put("messageId",messageId);*/
//                        publishDeviceMessageToRabbitmq(restartMap,RabbitmqConstant.SENT_TOPIC_PREFIX+enumType+RabbitmqConstant.SENT_TOPIC_RESTART);
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }
                    //同步围栏信息  圆形范围：POSTSELF
                    if(CommandEnum.POSTSELF.name().equals(enumType)){
                        /*Map<String, Object> circularMap = (Map<String, Object>)obj.getOutput();
                        circularMap.put("enumType",enumType);
                        circularMap.put("deviceId",deviceId);
                        circularMap.put("messageId",messageId);*/
//                        publishDeviceMessageToRabbitmq(circularMap,RabbitmqConstant.SENT_TOPIC_PREFIX+enumType+RabbitmqConstant.SENT_TOPIC_CIRCULAR);
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }
                    //同步围栏信息  B、多边形范围：POSTSELFM
                    if(CommandEnum.POSTSELFM.name().equals(enumType)){
                       /* Map<String, Object> polygonMap = (Map<String, Object>)obj.getOutput();
                        polygonMap.put("enumType",enumType);
                        polygonMap.put("deviceId",deviceId);
                        polygonMap.put("messageId",messageId);*/
//                        publishDeviceMessageToRabbitmq(polygonMap,RabbitmqConstant.SENT_TOPIC_PREFIX+enumType+RabbitmqConstant.SENT_TOPIC_POLYGON);
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }


                    //白名单功能开关
                    if(CommandEnum.CALLFILTER.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }

                    //设置免打扰时间协议
                    if(CommandEnum.CLASS.name().equals(enumType)){
                        /*Map<String, Object> classMap = (Map<String, Object>)obj.getOutput();
                        classMap.put("enumType",enumType);
                        classMap.put("deviceId",deviceId);
                        classMap.put("messageId",messageId);*/
//                        publishDeviceMessageToRabbitmq(classMap,RabbitmqConstant.SENT_TOPIC_PREFIX+enumType+RabbitmqConstant.SENT_TOPIC_DISTURBCONFIG);
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }
                    //设置免打扰时间开关
                    if(CommandEnum.CLASSSWITCH.name().equals(enumType)){
                        /*Map<String, Object> classswitchMap = (Map<String, Object>)obj.getOutput();
                        classswitchMap.put("enumType",enumType);
                        classswitchMap.put("deviceId",deviceId);
                        classswitchMap.put("messageId",messageId);*/
//                        publishDeviceMessageToRabbitmq(classswitchMap,RabbitmqConstant.SENT_TOPIC_PREFIX+enumType+RabbitmqConstant.SENT_TOPIC_DISTURBSWFLAG);
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }

                    //设置ID切换
                    if(CommandEnum.CHOOSEID.name().equals(enumType)){
                        /*Map<String, Object> chooseidMap = (Map<String, Object>)obj.getOutput();
                        chooseidMap.put("enumType",enumType);
                        chooseidMap.put("deviceId",deviceId);
                        chooseidMap.put("messageId",messageId);*/
//                        publishDeviceMessageToRabbitmq(chooseidMap,RabbitmqConstant.SENT_TOPIC_PREFIX+enumType+RabbitmqConstant.SENT_TOPIC_CHOOSEID);
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }

                    //查询设备中sim卡iccid
                    if(CommandEnum.GETICCID.name().equals(enumType)){
                       /* Map<String, Object> getIccIdMap = (Map<String, Object>)obj.getOutput();
                        getIccIdMap.put("enumType",enumType);
                        getIccIdMap.put("deviceId",deviceId);
                        getIccIdMap.put("messageId",messageId);*/
//                        publishDeviceMessageToRabbitmq(getIccIdMap,RabbitmqConstant.SENT_TOPIC_PREFIX+enumType+RabbitmqConstant.SENT_TOPIC_GETICCID);
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }

                    //查询设备IMEI号
                    if(CommandEnum.GETIMEID.name().equals(enumType)){
                       /* Map<String, Object> getImeIdMap = (Map<String, Object>)obj.getOutput();
                        getImeIdMap.put("enumType",enumType);
                        getImeIdMap.put("deviceId",deviceId);
                        getImeIdMap.put("messageId",messageId);*/
//                        publishDeviceMessageToRabbitmq(getImeIdMap,RabbitmqConstant.SENT_TOPIC_PREFIX+enumType+RabbitmqConstant.SENT_TOPIC_GETIMEID);
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }

                    //呼叫时长限制
                    if(CommandEnum.CALLDURATION.name().equals(enumType)){
                       /* Map<String, Object> callRecordMap = (Map<String, Object>)obj.getOutput();
                        callRecordMap.put("enumType",enumType);
                        callRecordMap.put("deviceId",deviceId);
                        callRecordMap.put("messageId",messageId);*/
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }


                    //主动获取定位命令
                    if(CommandEnum.D1.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }

                    //要求设备上报一次 WIFI
                    if(CommandEnum.D2.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }


                    //关机
                    if(CommandEnum.GJ.name().equals(enumType)){
                       /* Map<String, Object> gjMap = (Map<String, Object>)obj.getOutput();
                        gjMap.put("enumType",enumType);
                        gjMap.put("deviceId",deviceId);
                        gjMap.put("messageId",messageId);*/
//                        publishDeviceMessageToRabbitmq(gjMap,RabbitmqConstant.SENT_TOPIC_PREFIX+enumType+RabbitmqConstant.SENT_TOPIC_GJ);
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }

                    //禁止用户关机
                    if(CommandEnum.GJBAN.name().equals(enumType)){
                       /* Map<String, Object> gjbanMap = (Map<String, Object>)obj.getOutput();
                        gjbanMap.put("enumType",enumType);
                        gjbanMap.put("deviceId",deviceId);
                        gjbanMap.put("messageId",messageId);*/
//                        publishDeviceMessageToRabbitmq(gjbanMap,RabbitmqConstant.SENT_TOPIC_PREFIX+enumType+RabbitmqConstant.SENT_TOPIC_GJBAN);
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }

                    //域名和端口设置
                    //当为设备更换服务器域名时使用此指令；设备收到此指令，则回复一条 V4，并在 10 秒内断开当前网络连接， 重连到新服务器域名和端口。如设置了域名，域名优先于 S23 设置的 iP 地址。
                    if(CommandEnum.S2DOMAIN.name().equals(enumType)){
                       /* Map<String, Object> s2domainMap = (Map<String, Object>)obj.getOutput();
                        s2domainMap.put("enumType",enumType);
                        s2domainMap.put("deviceId",deviceId);
                        s2domainMap.put("messageId",messageId);*/
//                        publishDeviceMessageToRabbitmq(s2domainMap,RabbitmqConstant.SENT_TOPIC_PREFIX+enumType+RabbitmqConstant.SENT_TOPIC_DOMAINNAMEPORT);
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }

                    //设置服务器 IP 地址、端口号
                    if(CommandEnum.S23.name().equals(enumType)){
                        /*Map<String, Object> s23Map = (Map<String, Object>)obj.getOutput();
                        s23Map.put("enumType",enumType);
                        s23Map.put("deviceId",deviceId);
                        s23Map.put("messageId",messageId);*/
//                        publishDeviceMessageToRabbitmq(s23Map,RabbitmqConstant.SENT_TOPIC_PREFIX+enumType+RabbitmqConstant.SENT_TOPIC_IPADDRPORT);
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }

                    //工作模式设置 工作模式：模式 1 为实时定位模式，模式 2 为省电模式，m3 模式为基站定位模式，m4 为短信模式。
                    if(CommandEnum.MODE.name().equals(enumType)){
                       /* Map<String, Object> modeMap = (Map<String, Object>)obj.getOutput();
                        modeMap.put("enumType",enumType);
                        modeMap.put("deviceId",deviceId);
                        modeMap.put("messageId",messageId);*/
//                        publishDeviceMessageToRabbitmq(modeMap,RabbitmqConstant.SENT_TOPIC_PREFIX+enumType+RabbitmqConstant.SENT_TOPIC_MODE);
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }

                    //学生信息同步  STUINFO
                    if(CommandEnum.STUINFO.name().equals(enumType)){
                       /* Map<String, Object> stuInfoMap = (Map<String, Object>)obj.getOutput();
                        stuInfoMap.put("enumType",enumType);
                        stuInfoMap.put("deviceId",deviceId);
                        stuInfoMap.put("messageId",messageId);*/
//                        publishDeviceMessageToRabbitmq(stuInfoMap,RabbitmqConstant.SENT_TOPIC_PREFIX+enumType+RabbitmqConstant.SENT_TOPIC_STUINFO);
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }

                    //监听号码设置命令  MONITOR
                    if(CommandEnum.MONITOR.name().equals(enumType)){
                        /*Map<String, Object> monitorMap = (Map<String, Object>)obj.getOutput();
                        monitorMap.put("enumType",enumType);
                        monitorMap.put("deviceId",deviceId);
                        monitorMap.put("messageId",messageId);*/
//                        publishDeviceMessageToRabbitmq(monitorMap,RabbitmqConstant.SENT_TOPIC_PREFIX+enumType+RabbitmqConstant.SENT_TOPIC_MONITOR);
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }

                    // 设置免打扰功能模式：CALLFUN
                    if(CommandEnum.CALLFUN.name().equals(enumType)){
                       /* Map<String, Object> callfunMap = (Map<String, Object>)obj.getOutput();
                        callfunMap.put("enumType",enumType);
                        callfunMap.put("deviceId",deviceId);
                        callfunMap.put("messageId",messageId);*/
//                        publishDeviceMessageToRabbitmq(callfunMap,RabbitmqConstant.SENT_TOPIC_PREFIX+enumType+RabbitmqConstant.SENT_TOPIC_MONITOR);
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }

                    // 设喇叭音量调节：SPEAKER
                    if(CommandEnum.SPEAKER.name().equals(enumType)){
                       /* Map<String, Object> speakerMap = (Map<String, Object>)obj.getOutput();
                        speakerMap.put("enumType",enumType);
                        speakerMap.put("deviceId",deviceId);
                        speakerMap.put("messageId",messageId);*/
//                        publishDeviceMessageToRabbitmq(speakerMap,RabbitmqConstant.SENT_TOPIC_PREFIX+enumType+RabbitmqConstant.SENT_TOPIC_SPEAKER);
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }

                    // TTS 语音音量设置：TTSLEVEL
                    if(CommandEnum.TTSLEVEL.name().equals(enumType)){
                        /*Map<String, Object> ttslevelMap = (Map<String, Object>)obj.getOutput();
                        ttslevelMap.put("enumType",enumType);
                        ttslevelMap.put("deviceId",deviceId);
                        ttslevelMap.put("messageId",messageId);*/
//                        publishDeviceMessageToRabbitmq(ttslevelMap,RabbitmqConstant.SENT_TOPIC_PREFIX+enumType+RabbitmqConstant.SENT_TOPIC_TTSLEVEL);
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }

                    //请求设备响铃 RING
                    if(CommandEnum.RING.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }

                    // 来电振铃音量设置：RINGLEVEL
                    if(CommandEnum.RINGLEVEL.name().equals(enumType)){
                        /*Map<String, Object> ringlevleMap = (Map<String, Object>)obj.getOutput();
                        ringlevleMap.put("enumType",enumType);
                        ringlevleMap.put("deviceId",deviceId);
                        ringlevleMap.put("messageId",messageId);*/
//                        publishDeviceMessageToRabbitmq(ringlevleMap,RabbitmqConstant.SENT_TOPIC_PREFIX+enumType+RabbitmqConstant.SENT_TOPIC_RINGLEVEL);
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }

                    // 通话音量设置：PHONELEVEL
                    if(CommandEnum.PHONELEVEL.name().equals(enumType)){
                        /*Map<String, Object> phonelevelMap = (Map<String, Object>)obj.getOutput();
                        phonelevelMap.put("enumType",enumType);
                        phonelevelMap.put("deviceId",deviceId);
                        phonelevelMap.put("messageId",messageId);*/
//                        publishDeviceMessageToRabbitmq(phonelevelMap,RabbitmqConstant.SENT_TOPIC_PREFIX+enumType+RabbitmqConstant.SENT_TOPIC_PHONELEVEL);
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }

                    //家庭 MAC 地址设置 HOMEMAC
                    if(CommandEnum.HOMEMAC.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }


                    // 家庭作业通知：HOMEWORK
                    if(CommandEnum.HOMEWORK.name().equals(enumType)){

                       /* Map<String, Object> phonelevelMap = (Map<String, Object>)obj.getOutput();
                        phonelevelMap.put("enumType",enumType);
                        phonelevelMap.put("deviceId",deviceId);
                        phonelevelMap.put("messageId",messageId);*/
//                        publishDeviceMessageToRabbitmq(phonelevelMap,RabbitmqConstant.SENT_TOPIC_PREFIX+enumType+RabbitmqConstant.SENT_TOPIC_PHONELEVEL);
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }


                    //平台主动获取计步数据 GETSTEP
                    if(CommandEnum.GETSTEP.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }

                    //限制运营商 MNC
                    if(CommandEnum.MNC.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }

                    //设置单个白名单号码指令 PBWL
                    if(CommandEnum.PBWL.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }

                    //设置多个白名单号码指令 PBWLALL
                    if(CommandEnum.PBWLALL.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }

                    //PDOP 的门限值设定 PDOP
                    if(CommandEnum.PDOP.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }

                    //SIM 卡锁定 PINLOCK
                    if(CommandEnum.PINLOCK.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }

                    //闹钟 REMIND
                    if(CommandEnum.REMIND.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }

                    //电子签到应用开关 RFSWITCH
                    if(CommandEnum.RFSWITCH.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }

                    //恢复出厂设置 S25
                    if(CommandEnum.S25.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }

                    //学校 MAC 地址设置 SCHOOLMAC
                    if(CommandEnum.SCHOOLMAC.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }

                    //短信发送 SENDSMS
                    if(CommandEnum.SENDSMS.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }

                    //闲置时间自动休眠 SLEEPTIME
                    if(CommandEnum.SLEEPTIME.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }

                    //发短信息给设备 SMSINFO
                    if(CommandEnum.SMSINFO.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }

                    //SOS 电话拨号开关 SOSFUN
                    if(CommandEnum.SOSFUN.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }

                    //静止报警使能开关 STATICSWITCH
                    if(CommandEnum.STATICSWITCH.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }

                    //天气通知 WEATHER
                    if(CommandEnum.WEATHER.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }

                    //设置单个白名单号码指令 WHITELIST
                    if(CommandEnum.WHITELIST.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }

                    //WIFI 定时扫描 WIFISCAN
                    if(CommandEnum.WIFISCAN.name().equals(enumType)){
                        publishDeviceMessageToRabbitmq(BizMessageUtil.buildBizMsgReplyFromFunctionInvokeMessageReply(obj));
                    }


                }else if(msg instanceof DirectDeviceMessage){//透传消息

                }
                return msg;
            }).onErrorContinue((err, obj) ->{
                    log.error("订阅设备消息并推送到rabbitmq异常，消息内容【{}】",obj);
                    log.error(err.getMessage(), err);
                })
            .then();
    }

    private void publishDeviceMessageToRabbitmq(DeviceBizMessageReply message){
        amqpTemplate.convertAndSend(RabbitmqConstant.IOT2BIZ_EXCHANGE,RabbitmqConstant.IOT2BIZ_BIND_KEY,message);
    }
}
